package com.sduept.nwld.dataserver.controller.ledger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.TreeNode;

import com.sduept.cim.ledger.enums.ManagementUnitEnum;
import com.sduept.cim.ledger.excetpion.ManagementUnitException;
import com.sduept.cim.ledger.model.ManagementUnitDict;
import com.sduept.cim.ledger.services.ManagementUnitService;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import com.sduept.bigdata.web.jsf.controller.AbstractController;

@Named
@ViewScoped
public class ManagementUnitController extends AbstractController {

	// TODO: 该类 更换了ManagementUnitService 实现，需要优化，暂时把错误去掉

	private static final long serialVersionUID = 7611911277749475659L;

	@Autowired
	private ManagementUnitService unitM;

	private TreeNode root;
	private TreeNode currentNode;

	private ManagementUnitDict unit = new ManagementUnitDict();
	private ManagementUnitDict selectedUnit;

	private int currentSelectOrder = -1;
	private String unitType;

	@PostConstruct
	public void init() {
		try {
			initNodes();
		} catch (ManagementUnitException e) {
			// TODO Auto-generated catch block
		}
	}

	private void initNodes() throws ManagementUnitException {
		root = new LibTreeNode();
		ManagementUnitEnum[] rootUnits = ManagementUnitEnum.values();
		if (rootUnits.length > 0) {
			for (ManagementUnitEnum unit : rootUnits) {
				TreeNode node = new LibTreeNode(unit.getLabel(), root);
				List<ManagementUnitDict> subEntity = unitM.getManagementUnitsByType(unit.name());
				if (subEntity.size() > 0) {
					for (ManagementUnitDict subUnit : subEntity) {
						new LibTreeNode(subUnit, node);
					}
				}
				node.setExpanded(true);
			}
		}
	}

	public void preCreate() {
		//		List<ManagementUnitDict> subUnits = unitM.getUnitsByPid(selectedUnit.getId());
		//		unitType = selectedUnit.getUnitType();
		//		unit = new ManagementUnitDict();
		//		unit.setUnitType(unitType);
		//		unit.setPid(selectedUnit.getId());
		//		unit.setOrdered(subUnits.size());
		//		currentSelectOrder = subUnits.size();
	}

	public void preEdit() {
		//		unitType = selectedUnit.getUnitType();
		unit = selectedUnit;
	}

	public void saveUnit() {
		//		unitM.createOrUpdate(unit);
		selectedUnit = null;
		currentNode = null;
		//		initNodes();
	}

	public void delete() {
		//		unitM.delete(selectedUnit);
		addSuccessMessage("删除成功！");
		//		initNodes();
	}

	public void onNodeSelect(NodeSelectEvent event) {
		currentNode = event.getTreeNode();
		if (currentNode.getData() != null && currentNode.getData() instanceof ManagementUnitDict) {
			selectedUnit = (ManagementUnitDict) currentNode.getData();
			currentSelectOrder = selectedUnit.getOrdered();
		} else {
			currentNode = null;
		}
	}

	public void onNodeExpand(NodeExpandEvent event) {
		currentNode = event.getTreeNode();
		ManagementUnitDict unit = (ManagementUnitDict) currentNode.getData();
		//		List<ManagementUnitDict> subUnitList = unitM.getUnitsByPid(unit.getId());
		//		for (ManagementUnitDict sub : subUnitList) {
		//			new LibTreeNode(currentNode.getType(), sub, currentNode);
		//		}
	}

	public void openUploadDialog() {
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("resizable", true);
		options.put("draggable", true);
		options.put("modal", true);
		options.put("width", 600);
		options.put("height", 200);
		Map<String, List<String>> params = new HashMap<>();
		List<String> param = new ArrayList<>();
		param.add("1"); // 设置去除前几行
		params.put("ingnoreRows", param);
		openDialog("/ui/common/upload_excel_define", params, options);
	}

	/**
	 * 文件上传
	 * @param event
	 */
	public void onSelectorChosen(SelectEvent event) {
		List<List<String>> objs = (List<List<String>>) event.getObject();
		List<Integer> errorList = new ArrayList<Integer>();
		int rowIndex = 0;
		//		try {
		//			for (List<String> list : objs) {
		//				// 0单位名称   1父级单位
		//				rowIndex++;
		//				ManagementUnitDict unit = new ManagementUnitDict();
		//				unit.setName(list.get(0));
		//				// 通过父级单位名称找到父级单位id
		//				ManagementUnitDict parentUnit = unitM.getUnitByName(list.get(1));
		//				if(null != parentUnit) {
		//					unit.setPid(parentUnit.getId());
		//					unit.setUnitType(parentUnit.getUnitType());
		//					// 获得父级单位下的子单位数量，以设置order
		//					List<ManagementUnitDict> units = unitM.getUnitsByPid(parentUnit.getId());
		//					unit.setOrdered(units.size());
		//					unitM.createOrUpdate(unit);
		//				} else {
		//					errorList.add(rowIndex);
		//				}
		//			}
		//			addSuccessMessage("文件上传成功！");
		//			StringBuffer sb = new StringBuffer();
		//			if(errorList.size()!=0) {
		//				sb.append("上传的文件中，第：");
		//				for (Integer i : errorList) {
		//					sb.append(i+"、");
		//				}
		//				sb.append("行，该单位的父级单位不存在，请核实后再次上传！");
		//				alertErrorMessage(sb+"");
		//			}
		//		}catch(Exception e) {
		//			e.printStackTrace();
		//			addErrorMessage("表格数字有误，请仔细核对！");
		//			return ;
		//		}
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentNode() {
		return currentNode;
	}

	public void setCurrentNode(TreeNode currentNode) {
		this.currentNode = currentNode;
	}

	public ManagementUnitDict getUnit() {
		return unit;
	}

	public void setUnit(ManagementUnitDict unit) {
		this.unit = unit;
	}

	public ManagementUnitDict getSelectedUnit() {
		return selectedUnit;
	}

	public void setSelectedUnit(ManagementUnitDict selectedUnit) {
		this.selectedUnit = selectedUnit;
	}

	public int getCurrentSelectOrder() {
		return currentSelectOrder;
	}

	public void setCurrentSelectOrder(int currentSelectOrder) {
		this.currentSelectOrder = currentSelectOrder;
	}

	public String getUnitType() {
		return unitType;
	}

	public void setUnitType(String unitType) {
		this.unitType = unitType;
	}

}
